/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Client;
import Shared.*;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;

/**
 *
 * @author belal
 */
public class ConversationGui extends javax.swing.JFrame {

    /**
     * Creates new form ConversationGui
     * @param clients
     * @param ident
     */
    ArrayList <ClientInt> clientsInt;
    String ident;
    ClientImp client;
    User[] friends;
    ArrayList<String> friendsMail;
    DefaultListModel<User> listModel;
    String userName;
    User meUser;
    User friendUser;
    public ConversationGui(ArrayList <ClientInt> clients,String ident,User[] friends,ArrayList<String> friendsMail,String user) {
        listModel = new DefaultListModel();
        initComponents();
        this.clientsInt=clients;
        this.ident=ident;
        this.friends=friends;
        this.friendsMail=friendsMail;
        userName=user;
        this.meUser=friends[0];
        try 
        {
            client = new ClientImp();
        } 
        catch (RemoteException ex) 
        {
            Logger.getLogger(ConversationGui.class.getName()).log(Level.SEVERE, null, ex);
        }
        for(int i=0;i<friends.length;i++)
        {   
            
            listModel.addElement( friends[i]);
        }
        selectedonlineusers.setModel(listModel);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane3 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jScrollPane1 = new javax.swing.JScrollPane();
        mainText = new javax.swing.JTextArea();
        jPanel1 = new javax.swing.JPanel();
        SenderTextF = new javax.swing.JTextField();
        SenderBttn = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        selectedonlineusers = new javax.swing.JList();
        jMenuBar1 = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        saveconv = new javax.swing.JMenuItem();
        history = new javax.swing.JMenuItem();
        exit = new javax.swing.JMenuItem();

        jList2.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane3.setViewportView(jList2);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Conversation");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setMaximumSize(new java.awt.Dimension(200, 300));
        setPreferredSize(new java.awt.Dimension(500, 600));

        mainText.setEditable(false);
        mainText.setColumns(20);
        mainText.setFont(new java.awt.Font("DejaVu Serif Condensed", 1, 13)); // NOI18N
        mainText.setRows(5);
        jScrollPane1.setViewportView(mainText);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel1.setBackground(new java.awt.Color(102, 5, 51));
        jPanel1.setLayout(new java.awt.BorderLayout());

        SenderTextF.setFont(new java.awt.Font("DejaVu Serif Condensed", 1, 13)); // NOI18N
        SenderTextF.setPreferredSize(new java.awt.Dimension(5, 27));
        SenderTextF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SenderTextFActionPerformed(evt);
            }
        });
        jPanel1.add(SenderTextF, java.awt.BorderLayout.CENTER);

        SenderBttn.setBackground(new java.awt.Color(255, 255, 255));
        SenderBttn.setFont(new java.awt.Font("DejaVu Serif Condensed", 1, 14)); // NOI18N
        SenderBttn.setForeground(new java.awt.Color(102, 5, 51));
        SenderBttn.setText("Send");
        SenderBttn.setMaximumSize(new java.awt.Dimension(58, 30));
        SenderBttn.setPreferredSize(new java.awt.Dimension(98, 30));
        SenderBttn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SenderBttnActionPerformed(evt);
            }
        });
        jPanel1.add(SenderBttn, java.awt.BorderLayout.LINE_END);

        getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);

        jPanel3.setBackground(new java.awt.Color(102, 5, 51));

        selectedonlineusers.setFont(new java.awt.Font("DejaVu Serif Condensed", 1, 14)); // NOI18N
        selectedonlineusers.setForeground(new java.awt.Color(102, 5, 51));
        selectedonlineusers.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "username 1", "username 2", "username 3", "username 4" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        selectedonlineusers.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                selectedonlineusersMouseClicked(evt);
            }
        });
        jScrollPane4.setViewportView(selectedonlineusers);

        jPanel3.add(jScrollPane4);

        getContentPane().add(jPanel3, java.awt.BorderLayout.LINE_END);

        jMenuBar1.setBackground(new java.awt.Color(102, 5, 51));
        jMenuBar1.setForeground(new java.awt.Color(255, 0, 127));

        fileMenu.setForeground(new java.awt.Color(102, 5, 51));
        fileMenu.setText("File");
        fileMenu.setFont(new java.awt.Font("DejaVu Serif Condensed", 1, 13)); // NOI18N

        saveconv.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        saveconv.setFont(new java.awt.Font("DejaVu Serif Condensed", 1, 13)); // NOI18N
        saveconv.setForeground(new java.awt.Color(102, 5, 51));
        saveconv.setMnemonic('S');
        saveconv.setText("Save Conversation");
        saveconv.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveconvActionPerformed(evt);
            }
        });
        fileMenu.add(saveconv);

        history.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_H, java.awt.event.InputEvent.CTRL_MASK));
        history.setFont(new java.awt.Font("DejaVu Serif Condensed", 1, 13)); // NOI18N
        history.setForeground(new java.awt.Color(102, 5, 51));
        history.setMnemonic('H');
        history.setText("Load History");
        history.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                historyActionPerformed(evt);
            }
        });
        fileMenu.add(history);

        exit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.CTRL_MASK));
        exit.setFont(new java.awt.Font("DejaVu Serif Condensed", 1, 13)); // NOI18N
        exit.setForeground(new java.awt.Color(102, 5, 51));
        exit.setMnemonic('E');
        exit.setText("Exit Conversation");
        exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitActionPerformed(evt);
            }
        });
        fileMenu.add(exit);

        jMenuBar1.add(fileMenu);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void SenderTextFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SenderTextFActionPerformed
        // TODO add your handling code here:
        if (SenderTextF.getText() !=null) {
            try {            
                //System.out.println("******"+friendArray.get(0).getUserName()+"*******");
                String msgBody = userName+":"+SenderTextF.getText();
                for(int k=0;k<clientsInt.size();k++)
                {
                    clientsInt.get(k).recieveGroupMessage(msgBody,ident,friends,friendsMail,friends[k].getUserName());
                }
            } catch (RemoteException ex) {
                Logger.getLogger(ChatWonePerson.class.getName()).log(Level.SEVERE, null, ex);
            }
            //mainText.setText(mainText.getText() +SenderTextF.getText() + '\n');
            SenderTextF.setText("");
        }
    }//GEN-LAST:event_SenderTextFActionPerformed

    private void saveconvActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveconvActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_saveconvActionPerformed

    private void historyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_historyActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_historyActionPerformed

    private void exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_exitActionPerformed

    private void SenderBttnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SenderBttnActionPerformed
        // TODO add your handling code here:
        if (SenderTextF.getText() !=null) {
            try {            
                //System.out.println("******"+friendArray.get(0).getUserName()+"*******");
                String msgBody = userName+":"+SenderTextF.getText();
                for(int k=0;k<clientsInt.size();k++)
                {
                    clientsInt.get(k).recieveGroupMessage(msgBody,ident,friends,friendsMail,friends[k].getUserName());
                }
            } catch (RemoteException ex) {
                Logger.getLogger(ChatWonePerson.class.getName()).log(Level.SEVERE, null, ex);
            }
            //mainText.setText(mainText.getText() +SenderTextF.getText() + '\n');
            SenderTextF.setText("");
        }
    }//GEN-LAST:event_SenderBttnActionPerformed

    private void selectedonlineusersMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_selectedonlineusersMouseClicked
        // TODO add your handling code here:
        if (evt.getClickCount() == 2) {
          int index = selectedonlineusers.locationToIndex(evt.getPoint());
          if (index >= 0) {
                friendUser = listModel.getElementAt(index);
                String ident=meUser.getUserMail()+":"+friendUser.getUserMail();
                client.checkIdent(ident,meUser,friendUser);
          }
        }
    }//GEN-LAST:event_selectedonlineusersMouseClicked
     public void receiveMessageInTextArea(String msg) {
        mainText.setText(mainText.getText() + msg + '\n');
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton SenderBttn;
    private javax.swing.JTextField SenderTextF;
    private javax.swing.JMenuItem exit;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenuItem history;
    private javax.swing.JList jList2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTextArea mainText;
    private javax.swing.JMenuItem saveconv;
    private javax.swing.JList selectedonlineusers;
    // End of variables declaration//GEN-END:variables
}
